f05021c346d9be7fdcf4c189a68996af272a7860,src/main/java/org/technicalsoftwareconfigurationmanagement/maven/plugin/XmlFormatter.java,XmlFormatter,format,#File#,240
Before Change
try {
inputStream = new FileInputStream(formatFile);
if (inputStream == null) {
getLog().error("[xml formatter] File<" + formatFile + "> could not be opened, skipping");
return;
}
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
xml = documentBuilder.parse(inputStream);
getLog().info("Successfully formatted file: " + formatFile);
} catch(Throwable t) {
throw new RuntimeException("[xml formatter] Failed to parse..." + t.getMessage(), t);
After Change
xml = documentBuilder.parse(inputStream);
getLog().debug("[xml formatter] Successfully parsed file: " + formatFile);
} catch(Throwable t) {
throw new RuntimeException("[xml formatter] Failed to parse..." + t.getMessage(), t);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch(Throwable tr) {
// intentially exception hiding for failures on close....
}
}
}
FileOutputStream fos = null;
InputStream stylesheet = null;
File tmpFile = null;
try {
// Read the stylesheet from the classpath
stylesheet = new XmlFormatter().getClass().getClassLoader()
.getResourceAsStream("remove-whitespace.xsl");
if (stylesheet == null) {
getLog().error("[xml formatter] Could not find remove-whitespace.xsl");
return;
}
tmpFile = File.createTempFile("xmlFormatter", ".xml");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(new StreamSource(stylesheet));
fos = new FileOutputStream(tmpFile);
StreamResult streamResult = new StreamResult(fos);
DOMSource domSource = new DOMSource(xml);
transformer.transform(domSource, streamResult);
} catch(Throwable t) {
if (tmpFile != null) {
tmpFile.delete();
}
throw new RuntimeException("[xml formatter] Failed to parse..." + t.getMessage(), t);
} finally {
if (stylesheet != null) {
try {
stylesheet.close();
} catch(Throwable tr) {
// intentially exception hiding for failures on close....
}
}
if (fos != null) {
try {
fos.close();
} catch(Throwable t) {
// intentially exception hiding for failures on close....
}
}
}
// Now that we know that the indent is set to four spaces, we can either
// keep it like that or change them to tabs depending on which 'mode' we
// are in.
if (useTabs) {
indentFile(tmpFile);
}
// Copy tmpFile to formatFile, but only if the content has actually changed
String tmpFileHash = getSha1(tmpFile);
String formatFileHash = getSha1(formatFile);
if (tmpFileHash != null && formatFileHash != null && tmpFileHash.equals(formatFileHash)) {
// Exact match, so skip
getLog().info("[xml formatter] File unchanged after formatting: " + formatFile);
tmpFile.delete();
return;
}
// To get here indicates a hash comparison failure, or the file has modified after formatting. Copy the bytes
FileInputStream source = null;
FileOutputStream destination = null;
try {
source = new FileInputStream(tmpFile);
destination = new FileOutputStream(formatFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = source.read(buffer)) != -1) {
destination.write(buffer, 0, bytesRead); // write
}
getLog().info("[xml formatter] File reformatted: " + formatFile);
} catch (IOException ioe) {
getLog().error("[xml formatter] File copying failed for: " + tmpFile + " -> " + formatFile);
} finally {